package com.tian.service.impl;

import com.tian.po.UserPO;
import com.tian.po.UserQueryPO;
import com.tian.repository.UserRepository;
import com.tian.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public List<UserPO> findByName(String name) {
        return userRepository.findByName(name);
    }

    @Override
    public UserPO findByNameAndAge(String name, int age) {
        return userRepository.findByNameAndAge(name, age);
    }

    @Override
    public List<UserPO> getUserByName(String name) {
        return userRepository.findUser(name);
    }

    @Override
    public List<UserPO> getAll(UserQueryPO query) {
        if(StringUtils.isEmpty(query.getName())){
            return userRepository.findAll();
        }else{
            return userRepository.findByName(query.getName());
        }
    }

    @Override
    public UserPO findById(long id) {
        return userRepository.findById(id).get();
    }

    @Override
    public void add(UserPO user) {
        userRepository.save(user);
    }

    @Override
    public void deleteById(long id) {
        userRepository.deleteById(id);
    }
}
